package com.mng.mybatistest;

import com.mng.mybatistest.mapper.EmployeeMapper;
import com.mng.mybatistest.mapper.EmployeeMapper2;
import com.mng.mybatistest.model.Employee;
import com.mng.mybatistest.service.EmployeeService;
import lombok.val;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@SpringBootTest
public class mybatistest {

    @Autowired
    EmployeeService employeeService;

    @Test
    public  void  test(){
        Employee employee = employeeService.getEmployeeById(1L);
        System.out.println(employee);
    }
    @Test
    public void mybatis3Test() throws FileNotFoundException {
        String configPath = "src/main/resources/mybatis-config.xml";
        InputStream inputStream = new FileInputStream(configPath);
        //SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream,"haha");
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*
           执行SQL语句 方法1
           只需要编写mapper.xml
           第一个参数 SQL statement 唯一标识符
           但缺少参数类型判断
         */
//        Employee employee = (Employee) sqlSession.selectOne("com.mng.mybatistest.mapper.EmployeeMapper.getEmployeeById", 1L);
//        System.out.println(employee);
//        sqlSession.close();
        /*
           执行SQL语句方法二   接口式编程
            mapper：为接口自动创建的动态代理对象。
           保证类型安全
         */
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getEmployeeById(1L);
        System.out.println(employee);
        sqlSession.close();
    }


    @Test
    public void mybatis3Test2() throws FileNotFoundException {
        String configPath = "src/main/resources/mybatis-config.xml";
        InputStream inputStream = new FileInputStream(configPath);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream,"development");
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getEmployeeByIdAndLastName(1L,"asdddd");
        System.out.println(employee);
        sqlSession.close();
    }
}
